﻿using System;
using System.Collections.Generic;
using System.Linq;
using TimeTracker.Entities;

namespace TimeTracker.Persistence
{
    public static class EntriesExtensions
    {
        public static IEnumerable<Entry> GetTodayEntries(this IQueryable<Entry> queryable)
        {
            var today = DateTime.Now.Date;
            var tomorrow = DateTime.Now.AddDays(1).Date;

            var query = from x in queryable
                        where x.LastStartTime > today && x.LastStartTime < tomorrow
                        select x;

            return query;
        }

        public static IEnumerable<Entry> GetTodayEntriesByUser(this IQueryable<Entry> queryable, User user)
        {
            return from x in queryable.GetTodayEntries()
                   where x.UserId == user.Id
                   select x;
        }

        public static IEnumerable<Entry> GetTodayEntriesByProject(this IQueryable<Entry> queryable, User project)
        {
            return from x in queryable.GetTodayEntries()
                   where x.ProjectId == project.Id
                   select x;
        }

        public static IEnumerable<Entry> GetEntriesByRange(this IQueryable<Entry> queryable, DateTime startTime, DateTime endTime)
        {
            var start = startTime.Date;
            var end = endTime.AddDays(1).Date;

            var query = from x in queryable
                        where x.LastStartTime > start && x.LastStartTime < end
                        select x;

            return query;
        }
    }
}
